package com.example.guiguiapplicatio.api;

import static com.example.guiguiapplicatio.api.GetCountTotalApi.ip;

import android.os.NetworkOnMainThreadException;
import android.util.Log;

import androidx.fragment.app.Fragment;
import com.example.guiguiapplicatio.api.Interface.GetExaminationRecordCallback;
import com.example.guiguiapplicatio.Response.ExaminationRecord;
import com.example.guiguiapplicatio.util.ResponseBody;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.io.IOException;
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Headers;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class GetExaminationRecordApi {
    private final Gson gson = new Gson();
    private GetExaminationRecordCallback getExaminationRecordCallback;
    private Fragment fragment;

    public GetExaminationRecordApi(Fragment fragment) {
        this.fragment = fragment;
    }

    public void setGetExaminationRecordCallback(GetExaminationRecordCallback getExaminationRecordCallback) {
        this.getExaminationRecordCallback = getExaminationRecordCallback;
    }

    public void performGetExaminationRecord(String idname, Integer starttime, List<String> listenername) {
        new Thread(() -> {
            String url = "http://"+ip +":9090/examination/recordlist";
            //  请求头
            Headers headers = new Headers.Builder()
                    .add("Accept", "application/json, text/plain, */*")
//                    .add("appId", "用户所申请的应用ID")
//                    .add("appSecret", "用户所申请的应用密钥")
                    .add("Content-Type", "application/json")
                    .build();
            Map<String, Object> bodyMap = new HashMap<>();
            bodyMap.put("idcardname", idname);
            bodyMap.put("starttime", starttime);
            bodyMap.put("listenername", listenername);
            String body=gson.toJson(bodyMap);
            MediaType MEDIA_TYPE_JSON = MediaType.parse("application/json; charset=utf-8");

            Request request = new Request.Builder()
                    .url(url)
                    // 将请求头加至请求中
                    .headers(headers)
                    .post(RequestBody.create(MEDIA_TYPE_JSON, body))
                    .build();
            try {
                OkHttpClient client = new OkHttpClient();
                client.newCall(request).enqueue(callback);
            } catch (NetworkOnMainThreadException ex) {
                ex.printStackTrace();
            }
        }).start();
    }
    private final Callback callback=new Callback() {
        @Override
        public void onFailure(Call call, IOException e) {
            e.printStackTrace();
            if(getExaminationRecordCallback!=null){
                getExaminationRecordCallback.onERFailure(e.getMessage());
            }
        }

        @Override
        public void onResponse(Call call, Response response) throws IOException {
            try {
                if (response.isSuccessful()) {
                    Type jsonType = new TypeToken<ResponseBody<List<ExaminationRecord>>>() {}.getType();
                    String body = response.body().string();
                    Log.d("info", body);
                    ResponseBody<List<ExaminationRecord>> dataResponseBody = gson.fromJson(body, jsonType);
                    Log.d("info", String.valueOf(dataResponseBody));
                    if (getExaminationRecordCallback != null) {
                        fragment.getActivity().runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                getExaminationRecordCallback.onERSuccess(dataResponseBody);
                            }
                        });
                    }
                } else {
                    if (getExaminationRecordCallback != null) {
                        getExaminationRecordCallback.onERFailure("Response was not successful: " + response.code());
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
                if (getExaminationRecordCallback != null) {
                    getExaminationRecordCallback.onERFailure(e.getMessage());
                }
            }
        }
    };
}
